# -*- codeing = utf-8 -*-
import requests
from bs4 import BeautifulSoup
for i in range(101):
    #这里设置了获取整个100页的数据，做了一个判断。
    a=0
    if i !=0 :
        a = i
        URL= f'https://cd.lianjia.com/ditiefang/pg{a}/'
        headers = {
            'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3877.400 QQBrowser/10.8.4506.400'
        }
        resp = requests.get(url=URL,headers=headers)
        resp.encoding = 'zh'
        if resp.status_code == 200:
            soup = BeautifulSoup(resp.text,'lxml')
            soup_list = soup.select('body > div.content > div.leftContent > ul.sellListContent > li > div')

            for i in soup_list:
                # if条件判断一个none的类型
                if i.select_one('div > div.title > a') != None:
                    #获取那个房屋类型
                    title_zt = i.select_one('div > div.title > a').text
                    #获取链接
                    title_href = i.select_one('div > div.title > a').attrs['href']
                    #获取价格
                    title_price = i.select_one('div > div.priceInfo > div > span').text
                    #title_price1 = i.select_one('div > div.priceInfo > div > i').text
                    #print(title_price)
                    # 获取位置
                    #title_address = i.select_one('div > div.flood > div.positionInfo > a').text
                    title_address1 = i.select_one('div > div.flood > div:nth-child(1)').text
                    title_address_1 = ''.join(title_address1.split())

                    #获取详细信息
                    title_xx = i.select_one('div > div.address > div:nth-child(1)').text
                    #去掉空格，让字体紧凑
                    title_xx_1 = ''.join(title_xx.split())
                    #print(title_xx_1)
                    #每平方米的价格
                    title_mi_price = i.select_one('div > div.priceInfo > div:nth-child(2) > span').text

                    #print(title_mi_price)

                    print(title_zt,title_href,title_price+'万',title_address_1,title_xx_1,title_mi_price)

                else:
                    print('*'*170)